Imports CHM2Word_Objects
Imports System.IO
Public Class frmChm2Word
    Dim WithEvents d As clsDecompileAndExport = New CHM2Word_Objects.clsDecompileAndExport()
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
        Try
            lblMessage.Visible = True
            Dim NameFile As String
            NameFile = txtCHM.Text
            Dim blnFile As Boolean
            blnFile = True
            If (Not File.Exists(NameFile)) Then
                blnFile = False
                If (Not Directory.Exists(NameFile)) Then
                    MessageBox.Show(Me, "Please enter a file", "File not found", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    txtCHM.Focus()
                    Return
                End If

            End If
            Dim files As List(Of String) = New List(Of String)
            If blnFile Then
                files.Add(NameFile)
            Else
                'folder
                For Each fileChm As String In Directory.GetFiles(NameFile, "*.chm")
                    files.Add(fileChm)
                Next
            End If

            For Each fileChm As String In files
                Dim NameFileDoc As String = Path.Combine(Path.GetDirectoryName(fileChm), Path.GetFileNameWithoutExtension(fileChm) + ".doc")
                d.DecompileAndExport(fileChm, NameFileDoc)
            Next
            'If (Not File.Exists(NameFileDoc)) Then
            '    MessageBox.Show(Me, "Error occured", "Not found " + NameFileDoc, MessageBoxButtons.OK, MessageBoxIcon.Error)
            'Else
            '    If (MessageBox.Show(Me, "Show the file?", NameFileDoc, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes) Then
            '        System.Diagnostics.Process.Start(NameFileDoc)
            '    End If
            'End If

        Catch ex As Exception
            MessageBox.Show(Me, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            lblMessage.Visible = False
        End Try


    End Sub

    Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        If (oflCHM.ShowDialog = Windows.Forms.DialogResult.OK) Then
            txtCHM.Text = oflCHM.FileName
        End If
    End Sub

    Private Sub d_ProcessFileIntoWord(ByVal sender As Object, ByVal e As CHM2Word_Objects.ProcessFileEventArgs) Handles d.ProcessFileIntoWord
        lblMessage.Text = e.FileNumberProcessed & " : " & e.FileNameProcessed
        lblMessage.Refresh()
    End Sub
End Class
